﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DTO;
namespace CungMuaC5.Products
{
    public partial class ThanhToan_3 : System.Web.UI.Page
    {
        BUS_KhuyenMai PickedProductDetail = new BUS_KhuyenMai();

        protected void Page_Load(object sender, EventArgs e)
        {
            DropDownList soluong;

            ThongTinNguoiNhan.DataSource = PickedProductDetail.GetDetails(Request.QueryString["MaKM"]);
            ThongTinNguoiNhan.DataBind();


            List<DTO_KhuyenMai> detail = PickedProductDetail.GetDetails(Request.QueryString["MaKM"]);
            Label dongia = (Label)ProductTable.FindControl("lblDonGia");
            dongia.Text = detail[0].DonGiaMoi.ToString();
            Label thanhtien = (Label)ProductTable.FindControl("lblThanhTien");
            soluong = (DropDownList)ProductTable.FindControl("SoLuong");

            DTO_NguoiNhan receiver = new DTO_NguoiNhan();
            DTO_DonHang dh = new DTO_DonHang();
            dh = Session.GetDonHang();
            if (Request.UrlReferrer.ToString().EndsWith("ThanhToan_2.aspx?makm=" + Request.QueryString["MaKM"]) || Request.UrlReferrer.ToString().EndsWith("ThanhToan_3.aspx?makm=" + Request.QueryString["MaKM"]))
            {
                if (!IsPostBack)
                    soluong.SelectedIndex = dh.SoLuong;
                receiver = Session.GetReceiver();
                Label txt = (Label)ThongTinNguoiNhan.FooterRow.FindControl("lblName");
                txt.Text = receiver.HoTen;
                txt = (Label)ThongTinNguoiNhan.FooterRow.FindControl("lblPhone");
                txt.Text = receiver.DienThoai;
                txt = (Label)ThongTinNguoiNhan.FooterRow.FindControl("lblAdr");
                if (receiver.DiaChi != null)
                    txt.Text = receiver.TP + " " + receiver.DiaChi;
                else
                    txt.Text = "Lầu 2, 180-182 Lý Chính Thống, Q.3";
                txt = (Label)ThongTinNguoiNhan.FooterRow.FindControl("lblGhiChu");
                txt.Text = dh.GhiChu;
                HyperLink mail = (HyperLink)ThongTinNguoiNhan.FooterRow.FindControl("EmailLink");
                mail.Text = receiver.Email;
                mail.NavigateUrl = "mailto:" + receiver.Email;

                txt = (Label)ThongTinNguoiNhan.FooterRow.FindControl("lblHinhThuc");
                switch (dh.HinhThucThanhToan)
                {
                    case 0:
                        if (dh.HinhThucNhanHang == 0)
                            txt.Text = "Thu tiền tại công ty";
                        else
                            txt.Text = "Thu tiền tại nhà";
                        break;
                    case 1:
                        txt.Text = "Thẻ ATM nội địa";
                        break;
                    case 2:
                        txt.Text = "Thẻ tín dụng";
                        break;
                }
            }

            thanhtien.Text = (Convert.ToInt32(dongia.Text) * Convert.ToInt32(soluong.SelectedValue.ToString())).ToString() + " đ";
            Label tong = (Label)ProductTable.FindControl("lblTong");
            tong.Text = thanhtien.Text;
            Label name = (Label)ProductTable.FindControl("lblName");
            name.Text = detail[0].TenKM;

            ImageButton imgbut = (ImageButton)ThongTinNguoiNhan.FindControl("ImageButtonDatMua");
            if (Session.GetCurrentUser() == null)
            {
                imgbut.ImageUrl = "~/Images/ThanhToan/YeuCauDangNhap.png";
                HyperLink hyper = (HyperLink)Master.Master.FindControl("DangNhap");
                imgbut.PostBackUrl = "JavaScript:void(0);";
                imgbut.Attributes.Add("onclick", "login(document.getElementById('" + hyper.ClientID + "'));return false;");
                Session.SetLoginTurnPage(1);
                /*DTO_DonHang donhang = new DTO_DonHang();
                donhang = Session.GetDonHang(); 
                donhang.KhuyenMai = int.Parse(Request.QueryString["MaKM"]);
                Session.SetDonHang(donhang);*/
            }
            /*else
                imgbut.Attributes.Add("onclick", "ImageButtonDatMua_Click");*/
        }

        protected void ImageButtonDatMua_Click(object sender, ImageClickEventArgs e)
        {
            DropDownList soluong = (DropDownList)ProductTable.FindControl("SoLuong");
            //soluong = (DropDownList)BuyProductTable1.FindControl("SoLuong");
            DTO_DonHang donhang = new DTO_DonHang();
            donhang = Session.GetDonHang(); 
            
            if (Session.GetCurrentUser() == null)
            {
                Session.SetLoginTurnPage(1);
                donhang.SoLuong = soluong.SelectedIndex;
                donhang.KhuyenMai = int.Parse(Request.QueryString["MaKM"]);
                Session.SetDonHang(donhang);
               //Response.Redirect("~/Account/Login.aspx");        
            }
            else
            {
                BUS_NguoiNhan kh = new BUS_NguoiNhan();
                int makh = kh.Insert(Session.GetReceiver());

                BUS_DonHang dh = new BUS_DonHang();
                
                donhang.KhuyenMai = int.Parse(Request.QueryString["MaKM"]);
                donhang.NgayDatHang = DateTime.Now;
                donhang.NguoiNhan = makh;
                donhang.SoLuong = soluong.SelectedIndex+1;
                donhang.TaiKhoan = Session.GetCurrentUser().MaTK;
                donhang.PhiGiaoHang = 0;
                donhang.TinhTrangThanhToan = 0;//0: chưa 1:rồi
                donhang.TinhTrang = 0;//0:chưa 1:rồi
                dh.Insert(donhang);
                List<DTO_DonHang> update=dh.SelectAll(Session.GetCurrentUser().MaTK.ToString());
                Session.SetDonHang(update[update.Count - 1]);
                if (donhang.HinhThucThanhToan != 0)
                {
                    List<DTO_KhuyenMai> detail = PickedProductDetail.GetDetails(Request.QueryString["MaKM"]);
                    //return
                    //cancel_return
                    string url = Request.Url.ToString();
                    url=url.Replace("ThanhToan_3", "ThanhToan_4");
                    Response.Redirect("https://www.sandbox.paypal.com/us/cgi-bin/webscr?cmd=_xclick&business=seller_1338117003_biz@gmail.com&item_name=" + detail[0].TenKM + "&item_number=" + Request.QueryString["MaKM"] + "&amount=" + (float)(detail[0].DonGiaMoi / 20000) * donhang.SoLuong + "&return=" + url + "&cancel_return=" + url);
                }
                else
                {
                    //Session.SetDonHang(null);
                    Response.Redirect("~/Products/ThanhToan_4.aspx?makm=" + Request.QueryString["MaKM"] + "&st=" + "InCompleted");
                }
            }
        }
    }
}